<template>
  <div class="container-subjects">
    <el-card>
      <!-- 筛选 -->
      <el-row>
        <!-- 表单 -->
        <el-col :span="18">
          <el-form lable-width="80px" inline size="small">
            <el-form-item label="学科名称">
              <el-input
                v-model="reqParams.subjectName"
                placeholder="搜索关键字"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="clear()">清除</el-button>
              <el-button type="primary" @click="search()">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <!-- 按钮 -->
        <el-button
          style="float: right"
          type="success"
          icon="el-icon-edit"
          size="small"
          @click="openDialog"
          >添加学科</el-button
        >
        <el-col :span="6"></el-col>
      </el-row>
      <!-- 总计 -->
      <el-alert
        :title="`数据一共 ${total} 条`"
        type="info"
        :closable="false"
        show-icon
        style="margin-bottom: 15px"
      ></el-alert>
      <!-- 表格 -->
      <el-table :data="subjects">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="学科名称" prop="subjectName"></el-table-column>
        <el-table-column label="创建者" prop="username"></el-table-column>
        <el-table-column label="创建日期" width="160px">
          <template slot-scope="scope">
            {{ scope.row.addDate | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column label="前台是否显示">
          <template slot-scope="scope">
            {{ scope.row.isFrontDisplay === 1 ? "是" : "否" }}
          </template>
        </el-table-column>
        <el-table-column
          label="二级目录"
          prop="twoLevelDirectory"
        ></el-table-column>
        <el-table-column label="标签" prop="tags"></el-table-column>
        <el-table-column label="题目数量" prop="totals"></el-table-column>
        <el-table-column label="操作" width="240px">
          <template slot-scope="scope">
            <el-link @click="toDiretory(scope.row)" :underline="false" type="primary">学科分类</el-link>
            <el-link @click="toTag(scope.row)" :underline="false" type="primary">学科标签</el-link>
            <el-link @click="openDialog(scope.row)" :underline="false" type="primary">修改</el-link>
            <el-link @click="delSubject(scope.row.id)" :underline="false" type="primary">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        class="pager"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :page-size="reqParams.pagesize"
        :current-page="reqParams.page"
        @current-change="changePager"
        @size-change="changeSize"
        :page-sizes="[5,10,20,50]"
      ></el-pagination>
    </el-card>
    <!-- 添加编辑学科组件 -->
    <subjects-add @upateList="upateList" :subject="currSubject" ref="subjectsAdd"></subjects-add>
  </div>
</template>

<script>
// 学科列表api
import { list as subjectsList, remove as subjectsDel } from '@/api/hmmm/subjects.js'
import SubjectsAdd from '../components/subjects-add'
export default {
  components: { SubjectsAdd },
  name: 'subjects-page',
  data () {
    return {
      // 筛选参数
      reqParams: {
        // 学科名字,关键字
        subjectName: null,
        // 页码
        page: 1,
        // 尺寸
        pagesize: 10
      },
      subjects: [],
      // 总条数
      total: 0,
      currSubject: {}
    }
  },
  created () {
    this.getSubjects()
  },
  methods: {
    // 删除学科
    delSubject (id) {
      this.$confirm('此操作将永久删除该学科,是否继续?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await subjectsDel({ id })
        if (this.subjects.length === 1) { // 判断删除的是不是最后一条数据,如果是让页码值减一
          this.reqParams.page = this.reqParams.page > 1 ? this.reqParams.page - 1 : 1
        }
        this.$message.success('删除成功')
        this.getSubjects()
      }).catch(e => {})
    },
    // 更新列表
    upateList () {
      if (!this.currSubject.id) {
        this.reqParams.page = 1
      }
      this.getSubjects()
    },
    // 打开对话框
    openDialog (row) {
      this.currSubject = row
      this.$nextTick(() => {
        this.$refs.subjectsAdd.open()
      })
    },
    // 去目录
    toDiretory (row) {
      const { id, subjectName: name } = row
      this.$router.push({ path: 'directorys', query: { id, name } })
    },
    // 去标签
    toTag (row) {
      const { id, subjectName: name } = row
      this.$router.push({ path: 'tags', query: { id, name } })
    },
    // 获取学科列表
    async getSubjects () {
      // api目录中的函数和接口和数据相关
      const { data } = await subjectsList(this.reqParams)
      // 给data中的数据赋值
      this.subjects = data.items
      this.total = data.counts
    },
    // 分页
    changePager (newPage) {
      this.reqParams.page = newPage
      this.getSubjects()
    },
    // 尺码
    changeSize (newSize) {
      this.reqParams.page = 1
      this.reqParams.pagesize = newSize
      this.getSubjects()
    },
    // 搜索
    search () {
      this.reqParams.page = 1
      this.getSubjects()
    },
    // 清除
    clear () {
      this.reqParams.subjectName = null
    }
  }
}

</script>

<style scoped lang='scss'>
.container-subjects {
  padding: 10px 15px;
  .pager {
    margin-top: 15px;
    margin-bottom: 15px;
    float: right;
  }
}
.el-link {
  margin-right: 10px;
}
</style>
